import { handleScrollTableConditions } from '../conditionFormat/useConditionFormat.js';

export function scrollTableFormat(dataSets) {
  const { conditionOptions, fieldNameChainMap, type, collectdataSetType, source } = dataSets;
  const data = source && source.map((obj) => Object.values(obj));
  let columns = [];
  source && source.map(item=>{
    Object.keys(item).map(key=>{
      !columns.includes(key) && columns.push(key)
    })
  })
  let sourceFields = [];
  let realData = []; // 原始数据
  let timeStampData = {}; // 时间类型数据
  let dimensionName = []; // 时间类型数据名称
  // 底层字段
  if (type !== 'STATIC') {
    sourceFields = Object.values(fieldNameChainMap?.xAxis || {});
    const typeNameList = Object.keys(collectdataSetType) && Object.keys(collectdataSetType).filter((type) => type === 'TIMESTAMP');
    dimensionName = typeNameList.map((name) => collectdataSetType[name]);
    // 获取时间类型的数据名称
    realData = source;
  }
  // 查找时间类型数据
  dimensionName.forEach((dimensionName) => {
    const result = realData.map((item) => {
      return {
        value: item[dimensionName],
      };
    });
    timeStampData[dimensionName] = result;
  });
  // 处理条件格式
  const componentAttrs = {
    conditionOptions,
    config: {
      header: columns,
      realData, // 原始数据
      sourceFields,
      data,
    },
    timeStampData,
  };
  handleScrollTableConditions(componentAttrs);
  return {
    formatData: {
      header: columns,
      realData, // 原始数据
      sourceFields,
      data,
    },
  };
}
